package com.jhgsys.internal.education.service.impl;

import com.jhgsys.internal.base.dto.BaseCautionaryLessonDto;
import com.jhgsys.internal.base.entity.BaseCautionaryLesson;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.FebsResponse;
import com.jhgsys.internal.common.utils.BeanHelper;
import com.jhgsys.internal.education.entity.EducationCautionaryLesson;
import com.jhgsys.internal.education.mapper.EducationCautionaryLessonMapper;
import com.jhgsys.internal.education.service.IEducationCautionaryLessonService;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import com.jhgsys.internal.system.entity.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author yj
 * @date 2024-09-13 17:53:22
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class EducationCautionaryLessonServiceImpl extends ServiceImpl<EducationCautionaryLessonMapper, EducationCautionaryLesson> implements IEducationCautionaryLessonService {

    /**
     * 查询（分页）
     * @author yj
     * @date 2024-09-13 17:53:22
     * @param request QueryRequest
     * @param educationCautionaryLesson educationCautionaryLesson
     * @return IPage<EducationCautionaryLesson>
    */
    @Override
    public IPage<EducationCautionaryLesson> findEducationCautionaryLessonPage(QueryRequest request, EducationCautionaryLesson educationCautionaryLesson) {
        LambdaQueryWrapper<EducationCautionaryLesson> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EducationCautionaryLesson::getDeletedFlg, 0);
        // TODO 设置查询条件
        if(StringUtils.isNotBlank(educationCautionaryLesson.getTenantId())){
            queryWrapper.like(EducationCautionaryLesson::getTenantId,educationCautionaryLesson.getTenantId());
        }
        if(StringUtils.isNotBlank(educationCautionaryLesson.getCautionaryTitle())){
            queryWrapper.like(EducationCautionaryLesson::getCautionaryTitle,educationCautionaryLesson.getCautionaryTitle());
        }
        if(StringUtils.isNotBlank(educationCautionaryLesson.getStudyDept())){
            queryWrapper.like(EducationCautionaryLesson::getStudyDept,educationCautionaryLesson.getStudyDept());
        }
        if(StringUtils.isNotBlank(educationCautionaryLesson.getCautionarySource())){
            queryWrapper.eq(EducationCautionaryLesson::getCautionarySource,educationCautionaryLesson.getCautionarySource());
        }
        if(StringUtils.isNotBlank(educationCautionaryLesson.getStudyDeptId())){
            queryWrapper.eq(EducationCautionaryLesson::getStudyDeptId,educationCautionaryLesson.getStudyDeptId());
        }
        if(StringUtils.isNotBlank(educationCautionaryLesson.getStudyUserNo())){
            queryWrapper.like(EducationCautionaryLesson::getStudyUserNo,educationCautionaryLesson.getStudyUserNo());
        }
        if(StringUtils.isNotBlank(educationCautionaryLesson.getStudyRealName())){
            queryWrapper.like(EducationCautionaryLesson::getStudyRealName,educationCautionaryLesson.getStudyRealName());
        }
        Page<EducationCautionaryLesson> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author yj
     * @date 2024-09-13 17:53:22
     * @param educationCautionaryLesson educationCautionaryLesson
     * @return List<EducationCautionaryLesson>
     */
    @Override
    public List<EducationCautionaryLesson> findEducationCautionaryLessonList(EducationCautionaryLesson educationCautionaryLesson) {
	    LambdaQueryWrapper<EducationCautionaryLesson> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EducationCautionaryLesson::getDeletedFlg, 0);
		// TODO 设置查询条件
        if(StringUtils.isNotBlank(educationCautionaryLesson.getTenantId())){
            queryWrapper.like(EducationCautionaryLesson::getTenantId,educationCautionaryLesson.getTenantId());
        }
        if(StringUtils.isNotBlank(educationCautionaryLesson.getCautionaryTitle())){
            queryWrapper.like(EducationCautionaryLesson::getCautionaryTitle,educationCautionaryLesson.getCautionaryTitle());
        }
        if(StringUtils.isNotBlank(educationCautionaryLesson.getStudyDept())){
            queryWrapper.like(EducationCautionaryLesson::getStudyDept,educationCautionaryLesson.getStudyDept());
        }
        if(StringUtils.isNotBlank(educationCautionaryLesson.getCautionarySource())){
            queryWrapper.eq(EducationCautionaryLesson::getCautionarySource,educationCautionaryLesson.getCautionarySource());
        }
        if(StringUtils.isNotBlank(educationCautionaryLesson.getStudyDeptId())){
            queryWrapper.eq(EducationCautionaryLesson::getStudyDeptId,educationCautionaryLesson.getStudyDeptId());
        }
        if(StringUtils.isNotBlank(educationCautionaryLesson.getStudyUserNo())){
            queryWrapper.like(EducationCautionaryLesson::getStudyUserNo,educationCautionaryLesson.getStudyUserNo());
        }
        if(StringUtils.isNotBlank(educationCautionaryLesson.getStudyRealName())){
            queryWrapper.like(EducationCautionaryLesson::getStudyRealName,educationCautionaryLesson.getStudyRealName());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author yj
    * @date 2024-09-13 17:53:22
    * @param id
    * @return EducationCautionaryLesson
    */
    @Override
    public BaseCautionaryLessonDto findEducationCautionaryLessonById(String id){
        EducationCautionaryLesson educationCautionaryLesson = this.baseMapper.selectById(id);
        BaseCautionaryLessonDto baseCautionaryLessonDto = BeanHelper.copyProperties(educationCautionaryLesson, BaseCautionaryLessonDto.class);
        return baseCautionaryLessonDto;
    }

    /**
     * 新增
     * @author yj
     * @date 2024-09-13 17:53:22
     * @param educationCautionaryLesson educationCautionaryLesson
     */
    @Override
    @Transactional
    public boolean createEducationCautionaryLesson(EducationCautionaryLesson educationCautionaryLesson) {
        return this.save(educationCautionaryLesson);
    }

    /**
     * 修改
     * @author yj
     * @date 2024-09-13 17:53:22
     * @param educationCautionaryLesson educationCautionaryLesson
     */
    @Override
    @Transactional
    public boolean updateEducationCautionaryLesson(EducationCautionaryLesson educationCautionaryLesson) {
        return this.updateById(educationCautionaryLesson);
    }

    /**
     * 逻辑删除
     * @author yj
     * @date 2024-09-13 17:53:22
     * @param educationCautionaryLesson educationCautionaryLesson
     * @param educationCautionaryLessonIds educationCautionaryLessonIds
     */
    @Override
    @Transactional
    public boolean deleteEducationCautionaryLesson(EducationCautionaryLesson educationCautionaryLesson,String educationCautionaryLessonIds) {
        boolean result = false;
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        List<String> list = Arrays.asList(educationCautionaryLessonIds.split(StringPool.COMMA));

        educationCautionaryLesson.setDeletedBy(user.getUserName());
        educationCautionaryLesson.setDeletedTime(new Date());
        educationCautionaryLesson.setDeletedFlg(FebsConstant.INVALID);
        int update = this.baseMapper.update(educationCautionaryLesson,new LambdaQueryWrapper<EducationCautionaryLesson>().in(EducationCautionaryLesson::getCautionarySourceId,list));
        if (update > 0 ){
            result = true;
        }
        return result;
	}

    /**
    * 物理删除
    * @author yj
    * @date 2024-09-13 17:53:22
    * @param educationCautionaryLessonIds educationCautionaryLessonIds
    */
    @Override
    @Transactional
    public boolean physicallyDeleteEducationCautionaryLesson(String educationCautionaryLessonIds) {
        List<String> list = Arrays.asList(educationCautionaryLessonIds.split(StringPool.COMMA));
        LambdaQueryWrapper<EducationCautionaryLesson> wapper = new LambdaQueryWrapper<>();
        wapper.in(EducationCautionaryLesson::getCautionarySourceId,list);
        return this.remove(wapper);
    }

    /**
     * 其他业务模块调用，保存警示信息
     *
     * @param tenantId
     * @param source          警示来源 1:事故管理 2：违章记分 3：隐患上报 4：安全检查
     * @param sourceId        业务数据主键id
     * @param sourceTitle     标题
     * @param stydyDeptId   需警示部门id
     * @param stydyDept 需警示部门名称
     * @param createBy 创建人
     */
    @Override
    public FebsResponse saveCautionaryLessonFromOthers(String tenantId, String source, String sourceId, String sourceTitle, String stydyDeptId, String stydyDept, String createBy){
        if(StringUtils.isBlank(tenantId)){
            return new FebsResponse().fail().message("未提供tenantId");
        }
        if(StringUtils.isBlank(source)){
            return new FebsResponse().fail().message("未提供警示来源");
        }
        if(StringUtils.isBlank(sourceId)){
            return new FebsResponse().fail().message("未提供业务数据主键id");
        }
        if(StringUtils.isBlank(sourceTitle)){
            return new FebsResponse().fail().message("未提供标题");
        }
        if(StringUtils.isBlank(stydyDeptId)){
            return new FebsResponse().fail().message("未提供需警示部门id");
        }
        EducationCautionaryLesson educationCautionaryLesson = new EducationCautionaryLesson();
        educationCautionaryLesson.setTenantId(tenantId);
        educationCautionaryLesson.setCautionarySource(source);
        educationCautionaryLesson.setCautionarySourceId(sourceId);
        educationCautionaryLesson.setCautionaryTitle(sourceTitle);
        educationCautionaryLesson.setStudyDeptId(stydyDeptId);
        educationCautionaryLesson.setStudyDept(stydyDept);
        educationCautionaryLesson.setStatus("0");
        educationCautionaryLesson.setCreatedBy(createBy);
        educationCautionaryLesson.setCreatedTime(new Date());
        educationCautionaryLesson.setDeletedFlg("0");
        this.save(educationCautionaryLesson);
        return new FebsResponse().success().message("添加警示成功");
    }
}
